package top.korori.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.korori.system.entity.bo.MyInfoBo;
import top.korori.system.entity.bo.PasswordBo;
import top.korori.system.entity.vo.SysLoginUserVo;
import top.korori.system.entity.vo.SysMenuVo;
import top.korori.system.service.SysMenuService;
import top.korori.system.service.SysMyService;
import top.korori.system.service.SysRoleService;
import top.korori.system.service.SysUserService;
import top.korori.system.utils.SysSecurityUtil;
import top.korori.common.model.Result;

import java.util.List;

@Api(tags = "我的信息")
@RestController
@RequestMapping("sys/my")
public class SysMyController {

    @Autowired
    private SysMyService sysMyService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("修改我的信息")
    @PutMapping("editMyInfo")
    public Result<Void> editMyInfo(@RequestBody @Validated MyInfoBo myInfoBo) {
        sysMyService.editMyInfo(myInfoBo);
        return Result.ok();
    }

    @ApiOperation("修改密码")
    @PutMapping("editPassword")
    public Result<Void> editPassword(@RequestBody @Validated PasswordBo passwordBo){
        sysMyService.editPassword(passwordBo);
        return Result.ok();
    }

    @ApiOperation("获取我的信息")
    @GetMapping("getUserInfo")
    public Result<SysLoginUserVo> getLoginUser(){
        SysLoginUserVo sysLoginUserVo = sysUserService.getLoginUserInfo();
        List<String> roleNames = sysRoleService.getMyRoleNames();
        sysLoginUserVo.setRoleNames(roleNames);
        return Result.ok(sysLoginUserVo);
    }

    @ApiOperation("获取我的菜单")
    @GetMapping("treeMenus")
    public Result<List<SysMenuVo>> treeMenus(){
        Long userId = SysSecurityUtil.getUserId();
        List<SysMenuVo> sysMenuVos = sysMenuService.treeByUserId(userId);
        return Result.ok(sysMenuVos);
    }



}
